from plugins.template import ParserTemplate
import re

def get_parsers():
	return {"Yumeko" : Parser}

class Parser(ParserTemplate):
	"""
	Grammar:
	Page = "p" Number|NumberRange .
	NumberRange = Number "-" Number .
	Text = JapaneseText Translation .

	panels unknown

	>>> from io import StringIO
	>>> text = '''[p25]
	... その後。。。  Afterwards…
	... そんなこんなで今では黒猫からさりげなく誘ってくれるくらいにまでなったんだが。。。  After all of that, Kuroneko now asks for it on her own…
	... [p27]
	... 「もう。。。赤ちゃんみたいよ？こんな小さい胸別に吸っても楽しくないでしょ？」”Geez…you’re like a baby.  It’s not very enjoyable sucking on such small breasts, is it?”'''
	>>> parser = Parser(StringIO(text))
	>>> parser.next_token()
	Token(page='25', panel='?', text='Afterwards…', line=1)
	>>> parser.next_token()
	Token(page='25', panel='?', text='After all of that, Kuroneko now asks for it on her own…', line=2)
	>>> parser.next_token()
	Token(page='27', panel='?', text='”Geez…you’re like a baby.  It’s not very enjoyable sucking on such small breasts, is it?”', line=4)
	>>> parser.next_token()
	"""
	_PageRe = re.compile('''\[p
	(.+)	# we do have number ranges
	\]$ # end''', re.VERBOSE | re.IGNORECASE)
	
	def __init__(self, file):
		super().__init__(file)
	
	def _parse_next(self, last_token):
		try:
			page = self._parse_re(Parser._PageRe) or last_token.page
			panel = "?"
			line = self._cur_line
			text = self._parse_text()
			return [self.Token(page, panel, text, line)]
		except self.ParserEndError:
			return [None]
	
	def _parse_text(self):
		text = self._get_line()
		self._next_line()
		return self._remove_japanese_chars(text)

if __name__ == '__main__':
	import doctest
	doctest.testmod()
